const path = require("path");
const webpack = require('webpack')
const WebpackBar = require('webpackbar');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const { yxScripts } = require("./utils")




const config = {
  mode: "development", // 指定构建环境  
  entry: yxScripts.findPath(), //入口文件

  output: {   // 出口
    path: path.join(__dirname, '../dist'),
    filename: "[name].[hash].vendors.js", //输出文件
    // publicPath: "/", // 打包后的资源的访问路径前缀
  },


  // 模块
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        exclude: /node_modules/,
        use: [
          'babel-loader',
          {
            loader: 'thread-loader',
            options: {
              workers: 2,
            },
            // 'happypack/loader',
          },
        ]

        // loaders: ['babel-loader'] //配置Babel来转换高级的ES语法
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader', // 创建 <style></style>
          },
          {
            loader: MiniCssExtractPlugin.loader,
          },
          {
            loader: 'css-loader',
            options: { importLoaders: 1 }
          },
          // {
          //   loader: "postcss-loader",
          //   options: {
          //     plugins: [
          //       require('autoprefixer')({
          //         overrideBrowserslist: ['last 5 version', '>1%', 'ios 7']
          //       })
          //     ]
          //   }
          // }
        ]
      },
      {
        test: /\.less$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
          },
          {
            loader: 'less-loader', // 编译 Less -> CSS
            // options: { javascriptEnabled: true}
          },
        ],
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,

        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 10000, // url-loader 包含file-loader，这里不用file-loader, 小于10000B的图片base64的方式引入，大于10000B的图片以路径的方式导入
              name: 'static/img/[name].[hash:7].[ext]'
            }
          },
          {
            loader: 'image-webpack-loader',
            options: {
              mozjpeg: {
                progressive: true,
                quality: 65,
              },
              // optipng.enabled: false will disable optipng
              optipng: {
                enabled: false,
              },
              pngquant: {
                quality: [0.65, 0.9],
                speed: 4,
              },
              gifsicle: {
                interlaced: false,
              },
              // the webp option will enable WEBP
              webp: {
                quality: 75,
              },
            }
          }
        ],

      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000, // 小于10000B的图片base64的方式引入，大于10000B的图片以路径的方式导入
          name: 'static/fonts/[name].[hash:7].[ext]'
        }
      },
      // {
      //   test: /\.m?js$/,
      //   exclude: /(node_modules|bower_components)/,
      //   use: {
      //     loader: 'babel-loader',
      //     options: {
      //       presets: ['@babel/preset-env', '@babel/preset-react'],
      //       plugins: ['@babel/plugin-proposal-object-rest-spread']
      //     }
      //   }
      // },
    ]
  },

  // 插件
  plugins: [
    new WebpackBar(),
    new webpack.ProvidePlugin({
      _: "lodash",
      $: "jQuery"
    }),
    new MiniCssExtractPlugin({
      filename: "index.css" //输出的css文件名，默认以入口文件名命名(例如main.css)
    }),
  ],

  resolve: {
    extensions: ['.js', '.jsx', 'ts', 'tsx', '.json'],  //表示这个个文件的后缀名可以不写
    alias: {
      "vue$": "vue/dist/vue.js", //修改导入包路径
      "@": path.join(__dirname, '../src'), //修改导入包路径
      "yun-engine": path.join(__dirname, '../src/yun-engine'), //修改导入包路径
    }
  },

  // 开发环境本地启动的服务配置
  devServer: {}
}



module.exports = config